<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="prev" href="module-select.html" />
<link rel="parent" href="module-select.html" />
<link rel="next" href="module-thread.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>15.1.1 Polling Objects </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="15.1 select  "
  href="module-select.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="15.1 select  "
  href="module-select.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="15.2 thread  "
  href="module-thread.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-select.html">15.1 select  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-select.html">15.1 select  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-thread.html">15.2 thread  </a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0017110000000000000000"></a><a name="poll-objects"></a>
<br>
15.1.1 Polling Objects
            
</h2>

<p>
The <tt class="cfunction">poll()</tt> system call, supported on most <span class="Unix">Unix</span> systems,
provides better scalability for network servers that service many,
many clients at the same time.
<tt class="cfunction">poll()</tt> scales better because the system call only
requires listing the file descriptors of interest, while <tt class="cfunction">select()</tt>
builds a bitmap, turns on bits for the fds of interest, and then
afterward the whole bitmap has to be linearly scanned again.
<tt class="cfunction">select()</tt> is O(highest file descriptor), while
<tt class="cfunction">poll()</tt> is O(number of file descriptors).

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3400' xml:id='l2h-3400' class="method">register</tt></b>(</nobr></td>
  <td><var>fd</var><big>[</big><var>, eventmask</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Register a file descriptor with the polling object.  Future calls to
the <tt class="method">poll()</tt> method will then check whether the file descriptor
has any pending I/O events.  <var>fd</var> can be either an integer, or an
object with a <tt class="method">fileno()</tt> method that returns an integer.  File
objects implement
<tt class="method">fileno()</tt>, so they can also be used as the argument.

<p>
<var>eventmask</var> is an optional bitmask describing the type of events you
want to check for, and can be a combination of the constants
<tt class="constant">POLLIN</tt>, <tt class="constant">POLLPRI</tt>, and <tt class="constant">POLLOUT</tt>,
described in the table below.  If not specified, the default value
used will check for all 3 types of events.

<p>
<div class="center"><table class="realtable">
  <thead>
    <tr>
      <th class="left"  >Constant</th>
      <th class="left"  >Meaning</th>
      </tr>
    </thead>
  <tbody>
    <tr><td class="left"   valign="baseline"><tt class="constant">POLLIN</tt></td>
        <td class="left"  >There is data to read</td></tr>
    <tr><td class="left"   valign="baseline"><tt class="constant">POLLPRI</tt></td>
        <td class="left"  >There is urgent data to read</td></tr>
    <tr><td class="left"   valign="baseline"><tt class="constant">POLLOUT</tt></td>
        <td class="left"  >Ready for output: writing will not block</td></tr>
    <tr><td class="left"   valign="baseline"><tt class="constant">POLLERR</tt></td>
        <td class="left"  >Error condition of some sort</td></tr>
    <tr><td class="left"   valign="baseline"><tt class="constant">POLLHUP</tt></td>
        <td class="left"  >Hung up</td></tr>
    <tr><td class="left"   valign="baseline"><tt class="constant">POLLNVAL</tt></td>
        <td class="left"  >Invalid request: descriptor not open</td></tr></tbody>
</table></div>

<p>
Registering a file descriptor that's already registered is not an
error, and has the same effect as registering the descriptor exactly
once.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3401' xml:id='l2h-3401' class="method">unregister</tt></b>(</nobr></td>
  <td><var>fd</var>)</td></tr></table></dt>
<dd>
Remove a file descriptor being tracked by a polling object.  Just like
the <tt class="method">register()</tt> method, <var>fd</var> can be an integer or an
object with a <tt class="method">fileno()</tt> method that returns an integer.

<p>
Attempting to remove a file descriptor that was never registered
causes a <tt class="exception">KeyError</tt> exception to be raised.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-3402' xml:id='l2h-3402' class="method">poll</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>timeout</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Polls the set of registered file descriptors, and returns a
possibly-empty list containing <code>(<var>fd</var>, <var>event</var>)</code> 2-tuples
for the descriptors that have events or errors to report.
<var>fd</var> is the file descriptor, and <var>event</var> is a bitmask
with bits set for the reported events for that descriptor
-- <tt class="constant">POLLIN</tt> for waiting input,
<tt class="constant">POLLOUT</tt> to indicate that the descriptor can be written to, and
so forth.
An empty list indicates that the call timed out and no file
descriptors had any events to report.
If <var>timeout</var> is given, it specifies the length of time in
milliseconds which the system will wait for events before returning.
If <var>timeout</var> is omitted, negative, or <tt class="constant">None</tt>, the call will
block until there is an event for this poll object.
</dl>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="15.1 select  "
  href="module-select.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="15.1 select  "
  href="module-select.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="15.2 thread  "
  href="module-thread.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-select.html">15.1 select  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-select.html">15.1 select  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="module-thread.html">15.2 thread  </a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
